package com.ruoyi.web.system.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IFanningdiService;
import com.ruoyi.system.service.IZhonggaofengxianquService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IFnryqkService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * fnryqkController
 * 
 * @author ruoyi
 * @date 2021-10-28
 */
@Controller
@RequestMapping("/system/fnryqk")
public class FnryqkController extends BaseController
{
    private String prefix = "system/fnryqk";

    @Autowired
    private IFnryqkService fnryqkService;

    @Autowired
    private IFanningdiService fanningdiService;

    @Autowired
    private IZhonggaofengxianquService zhonggaofengxianquService;

    @RequiresPermissions("system:fnryqk:view")
    @GetMapping()
    public String fnryqk()
    {
        return prefix + "/fnryqk";
    }

    /**
     * 查询fnryqk列表
     */
    @RequiresPermissions("system:fnryqk:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Fnryqk fnryqk)
    {
        startPage();
        List<Fnryqk> list = fnryqkService.selectFnryqkList(fnryqk);
        return getDataTable(list);
    }

    /**
     * 查询fnryqk列表
     */
    //@RequiresPermissions("system:fnryqk:customlist")
    @PostMapping("/customlist")
    @ResponseBody
    public TableDataInfo customlist(FnryqkSelect fnryqk)
    {
        startPage();
        List<Map<String,Object>> list = fnryqkService.selectCustomFnryqkList(fnryqk);
        return getDataTable(list);
    }

    /**
     * 导出fnryqk列表
     */
    @RequiresPermissions("system:fnryqk:export")
    @Log(title = "fnryqk", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FnryqkSelect fnryqk)
    {
        List<FnryqkCustom> list = fnryqkService.ExportCustom(fnryqk);
        ExcelUtil<FnryqkCustom> util = new ExcelUtil<FnryqkCustom>(FnryqkCustom.class);
        return util.exportExcel(list, "返宁人员情况");
    }

    /**
     * 新增fnryqk
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存fnryqk
     */
    //@RequiresPermissions("system:fnryqk:add")
    @Log(title = "fnryqk", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Fnryqk fnryqk,String fanningdi,String gaofengxiandis)
    {
        fnryqk.setId(UUID.randomUUID().toString());
        fnryqk.setChuangjiantime(new Date());
        fnryqk.setStatus("1");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = fnryqkService.selectFnryqkByUserIdAndFanningtime(fnryqk.getUserid(),sdf.format(fnryqk.getFanningtime()));
        if(list.size()>0){
            return error("该反宁时间已上报，如需修改，请删除原有填报");
        }

        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,1);//这里改为1
        Date tomorrow = cal.getTime();

        if (fnryqk.getFanningtime().compareTo(tomorrow)>0){
            return error("请勿提前上报返宁情况。");
        }

        return toAjax(fnryqkService.insertFnryqk(fnryqk,fanningdi,gaofengxiandis));
    }

    /**
     * 修改fnryqk
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Fnryqk fnryqk = fnryqkService.selectFnryqkById(id);

        String fnd = "";
        Fanningdi mmFnd = new Fanningdi();
        mmFnd.setFnryqkid(id);
        List<Fanningdi> mmFndList = fanningdiService.selectFanningdiList(mmFnd);
        if (mmFndList.size()==1){
            fnd = mmFndList.get(0).getFanningdi();
        }

        String zgd = "";
        Zhonggaofengxianqu mmZgd = new Zhonggaofengxianqu();
        mmZgd.setFnryqkid(id);
        List<Zhonggaofengxianqu> mmZgdList = zhonggaofengxianquService.selectZhonggaofengxianquList(mmZgd);
        if (mmZgdList.size()==1){
            zgd = mmZgdList.get(0).getZhonggaofengxiandi();
        }

        fnryqk.setText1(fnd);
        fnryqk.setText2(zgd);
        mmap.put("fnryqk", fnryqk);
        return prefix + "/edit";
    }

    /**
     * 修改保存fnryqk
     */
    @RequiresPermissions("system:fnryqk:edit")
    @Log(title = "fnryqk", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Fnryqk fnryqk,String fanningdi,String gaofengxiandis)
    {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        List<String> list = fnryqkService.selectFnryqkByUserIdAndFanningtime(fnryqk.getUserid(),sdf.format(fnryqk.getFanningtime()));
//        if(list.size()>0){
//            return error("该反宁时间已上报，如需修改，请删除原有填报");
//        }
        return toAjax(fnryqkService.updateFnryqk(fnryqk,fanningdi,gaofengxiandis));
    }

    /**
     * 删除fnryqk
     */
    //@RequiresPermissions("system:fnryqk:remove")
    @Log(title = "fnryqk", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fnryqkService.deleteFnryqkByIds(ids));
    }
}
